import pandas as pd
import numpy as np
import seaborn as sns
import xgboost as xgb
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler,PowerTransformer
from sklearn.linear_model import LinearRegression,LassoCV,LogisticRegression
from sklearn.ensemble import RandomForestClassifier,RandomForestRegressor
from sklearn.model_selection import KFold,train_test_split,StratifiedKFold,GridSearchCV,cross_val_score
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score,accuracy_score, \
                            precision_score,recall_score, roc_auc_score
import warnings
warnings.filterwarnings('ignore')

# 数据导入
# Give me some credit
'''
train_df = pd.read_csv(r'D:/based-saafm/ESC.pytorch/data/user_train.csv')
test_df = pd.read_csv(r'D:/based-saafm/ESC.pytorch/data/user_test.csv')

X = train_df.drop(['SeriousDlqin2yrs'],axis=1)
y = train_df['SeriousDlqin2yrs']
W = test_df.drop(['SeriousDlqin2yrs'],axis=1)
z = test_df['SeriousDlqin2yrs']
'''
# UCI_Credit_Card

train_df = pd.read_csv(r'/ESC.pytorch/data/UCI_Credit_Card_train.csv')
test_df = pd.read_csv(r'/ESC.pytorch/data/UCI_Credit_Card_test.csv')

X = train_df.drop(['default.payment.next.month'],axis=1)
y = train_df['default.payment.next.month']
W = test_df.drop(['default.payment.next.month'],axis=1)
z = test_df['default.payment.next.month']

cv = StratifiedKFold(n_splits=3,shuffle=True)
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=None)
score = ['roc_auc','accuracy','f1']
# 分类模型性能查看函数
def perfomance_clf(model,X,y,name=None):
    y_predict = model.predict(X)
    if name:
        print(name,':')
    print(f'accuracy score is: {accuracy_score(y,y_predict)}')
    print(f'precision score is: {precision_score(y,y_predict)}')
    print(f'recall score is: {recall_score(y,y_predict)}')
    print(f'auc: {roc_auc_score(y,y_predict)}')
    print('- - - - - - ')

# xgboost模型
xgb_clf = xgb.XGBClassifier(objective='binary:logistic',
                            n_job=-1,
                            booster='gbtree',
                            n_estimators=1000,
                            learning_rate=0.01)
# 参数设定
xgb_params = {'max_depth':[6,9],
             'subsample':[0.6,0.9],
             'colsample_bytree':[0.5,0.6],
             'reg_alpha':[0.05,0.1]}
# 参数搜索
xgb_gridsearch = GridSearchCV(xgb_clf,xgb_params,cv=cv,n_jobs=-1,
                                 scoring='roc_auc',verbose=10,refit=True)
# 工作流管道
pipe_xgb = Pipeline([
    ('sc',StandardScaler()),
    ('pow_trans',PowerTransformer()),
    ('xgb_grid',xgb_gridsearch)
])
# 搜索参数并训练模型
pipe_xgb.fit(X_train,y_train)
# 最佳参数组合
print(pipe_xgb.named_steps['xgb_grid'].best_params_)
# 训练集性能指标
perfomance_clf(pipe_xgb,X_train,y_train,name='train')
# 测试集性能指标
perfomance_clf(pipe_xgb,X_test,y_test,name='test')

for i in score:
    print(i,cross_val_score(pipe_xgb,X_train,y_train,cv=3,scoring=i).mean())

print('xgboost_max_auc:',cross_val_score(pipe_xgb,X_train,y_train,cv=3,scoring='roc_auc').max())
